package org.thanos.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Component;
import org.thanos.demo.model.ChatMessage;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import java.util.stream.Stream;

@Slf4j
@Component
public class WebSocketApplicationListener implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private SimpMessageSendingOperations messagingTemplate;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {

        for (int i = 0; i < 2000; i++) {
            CompletableFuture.runAsync(() -> {
                for (; ; ) {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    log.info("number");

                    ChatMessage chatMessage = new ChatMessage();
                    chatMessage.setType(ChatMessage.MessageType.CHAT);
                    chatMessage.setSender(UUID.randomUUID().toString());
                    chatMessage.setContent(UUID.randomUUID().toString());
                    messagingTemplate.convertAndSend("/topic/public", chatMessage);
                }

            });
        }

    }
}
